//
//  UserFeedbackViewController.m
//  HeXun
//
//  Created by ibokan on 15/11/17.
//  Copyright © 2015年 LGC. All rights reserved.
//

#import "UserFeedbackViewController.h"

//遵守单行文本框协议
@interface UserFeedbackViewController ()<UITextFieldDelegate>
{
    //用户反馈的内容的多行文本框
    __weak IBOutlet UITextView *_feedbackContentTextView;
    
    //用户联系方式的单行文本框
    __weak IBOutlet UITextField *_contactInformationTextField;
    
    
}
@end

@implementation UserFeedbackViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //设置代理人
    _contactInformationTextField.delegate = self;
    
    //用户输入联系方式时触发的方法
    [_contactInformationTextField addTarget:self action:@selector(userContactInformationTextFieldDidChanged:) forControlEvents:UIControlEventEditingChanged];
    
}

#pragma mark - "提交反馈" 按钮触发的方法
- (IBAction)submitUserFeedback:(UIButton *)sender
{
    //弹框
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提醒" message:@"您反馈的意见我们已经收到，感谢您的信任与支持！有您的支持，我们会做得更好！" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
}


#pragma mark - 单行文本框回收键盘
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return NO;
}


#pragma mark - 多行文本框、单行文本框都可以回收键盘
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_feedbackContentTextView resignFirstResponder];
    [_contactInformationTextField resignFirstResponder];
}


#pragma mark - 限制用户联系方式的输入（ 只能包含大小写字母、数字、点号(.)、@ ）
-(void)userContactInformationTextFieldDidChanged:(UITextField *)textField
{
    
    if (_contactInformationTextField.isEditing)
    {
        //判断输入的文字是否为汉字
        for (int i = 0; i < textField.text.length; i++)
        {
            //循环提取每个字符
            unichar c = [textField.text characterAtIndex:i];
            
            //判断是否为汉字
            if (c >= 0x4e00 && c <= 0x9fff)
            {
                NSLog(@"不能输入汉字");
                
                //如果输入的是汉字，则文本框只显示汉字前面的字符
                if (i == 0)
                {
                    _contactInformationTextField.text = @"";
                }
                else
                {
                    _contactInformationTextField.text = [textField.text substringToIndex:i];
                }
                
            }
        }
        
        //限制最大长度为20
        if (textField.text.length > 20)
        {
            _contactInformationTextField.text = [textField.text substringToIndex:20];
        }
        
    }
    
}






- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
